#ifndef CLUSNET_NET_TCPSOCKET_H
#define CLUSNET_NET_TCPSOCKET_H

#include <boost/asio.hpp>

namespace clusnet
{
namespace net
{

class TcpSocket final 
{
    public:
        TcpSocket(boost::asio::io_context& io_context)
            : socket_(io_context)
        {
        }

        ~TcpSocket()
        {
            socket_.close();
        }

        TcpSocket(const TcpSocket&) = delete;
        TcpSocket& operator= (const TcpSocket&) = delete;

    public:
        // 默认设置tcp_nodelay选项
        void setNoDelay(bool flag = true)
        {
            socket_.set_option(boost::asio::ip::tcp::no_delay(flag));
        }

        // 默认关闭keep alive选项
        void setKeepAlive(bool flag = false)
        {
            socket_.set_option(boost::asio::socket_base::keep_alive(flag));
        }

        void shutdownWrite()
        {
            socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_send);
        }

        boost::asio::ip::tcp::socket& socket()
        {
            return socket_;
        }

        void close()
        {
            socket_.close();
        }

        boost::asio::ip::tcp::endpoint localEndpoint() const
        {
            return socket_.local_endpoint();
        }

        boost::asio::ip::tcp::endpoint remoteEndpoint() const
        {
            return socket_.remote_endpoint();
        }

    private:
        boost::asio::ip::tcp::socket    socket_;
};

}
}

#endif 
